﻿var username;
var secretkey;
var countdown=60;
(function($){
	$(window).load(function(){
		find_staff();
	})
})(jQuery);

//工位全选
function check1(){
	var check = document.getElementById("vip");
	var s = document.getElementById("vips");
	var n = s.getElementsByTagName("input");
	for(i = 0;i < n.length;i++){//循环数组
		if (check.checked == false) {
			n[i].checked = false;
		} else{
			n[i].checked = true;
		}
	}
}

//获取所有工位
function find_staff(){
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
	$("#shop_id").html("");
		$.ajax({
			url: "../../../../service",
			data: {
				proc: "g_xmy.find_stationAll",
				para: "['" + username + "','" + secretkey + "']"
			},
			dataType: "json",
			success: function(data) {
				listCount = data.length;
				/*var len = $("#vips").length;
				if(len>0){
					$("#vips").empty();
				}*/
				if(data[0].TAG == "0"){
					window.location.href="./error.html";
				}else{
					var options="<option value='0'>请选择</option>";
					for(var a=0;a<data.length;a++){
						/*var al = document.getElementById("vips");
						var div =document.createElement("li");
						div.innerHTML=	'<label class="check-box" style="float:left;margin-left:20px;">
						<input type="checkbox" name="hdf" value="'+data[a].ID+'"/><span>'+data[a].NAME+'</span></label>'
						al.appendChild(div);*/
						options+='<option value="'+data[a].ID+'">'+data[a].NAME+'</option>';
					}
					$("#shop_id").html(options);
				}
			}
		});
}


//工号是否合法
function there_exist(){
	var login_name = document.getElementById('login_name').value;
/*	if (!(/^[0-9A-Za-z_]{5,12}$/.test(login_name))) {
		alert("登录名名格式有误，提示：5-12位字母或字母加数字。");
		return false;
	}	*/
	if (!(/^[0-9]{4,11}$/.test(login_name))) {
		layer.alert("工号名格式有误，提示：4-11位数字。");
		return false;
	}
	$.ajax({
		url: "../../../../service",
		data: {
			proc: "g_xmy.there_exist",
			para: "['" + login_name + "']"
		},
		dataType: "json",
		success: function(data) {
			if(data[0].TAG == "0"){
				document.getElementById("is_verification").innerHTML='<i style="color:red;">账号已存在，请重新输入！</i>';
			}else if(data[0].TAG == "1"){
				document.getElementById("is_verification").innerHTML='<i style="color:green;">账号可以使用！</i>';
			}else{
				layer.alert("请填写登录名");
			}
		}
	});
}
//添加员工
function add_staff_management(){ 
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
	var login_name = document.getElementById('login_name').value;
	var staff_name = document.getElementById('staff_name').value;

	/*var ac="";
	var a = document.getElementsByTagName("input"); 
	for(var b=0;b<a.length;b++){ 
		if(a[b].getAttribute("type")=="checkbox"){
			if(a[b].checked==true){ 
				ac += a[b].value+",";
			}
		}
	}
	var jurisdiction = ac.substring(0,ac.length-1);*/
	var jurisdiction=$("#shop_id option:selected").val(); //工位
//	alert("工位=="+jurisdiction);
	
	if (staff_name == '') {
		layer.alert("请输入姓名！");
		return false;
		//如果使用return，js的代码将继续执行。这是为什么？
	}
	if (!checkChinese(staff_name)) {
		layer.alert("请输入汉字，并且长度为2-8位");
		return false;
	}
	
	if(jurisdiction == '' && jurisdiction.length<=0){
		layer.alert("请为员工选择工位");
		return false;
	}
//	console.log(username + "','" + secretkey + "','" + login_name + "','" + staff_name  + "','" + jurisdiction);
	$.ajax({
		url: "../../../../service",
		cache : false,
		data : {
			//proc : "g_xmy.add_staff_management",
			proc : "xmy_zyr.add_staff_management",
			para : "['" + username + "','" + secretkey + "','" + login_name + "','" + staff_name  + "','" + jurisdiction + "']"
		},
		dataType : "json",
		success : function(data) {
		//	console.log(JSON.stringify(data));
			if (data[0].TAG == "1") {
				/*layer.alert("员工添加成功。",function(index){
					window.location.href="./factory_list.html";
					layer.close(index);
				});*/
				scan_code(login_name,data[0].ORDER_ID);
			} else {
				layer.alert(data[0].MSG);
			}
		}
	});
}

function scan_code(login_name,order_id){//扫码支付
	$.ajax({
		url : "/guanjias/Scancode_Pay",
		cache : false,
		data : {
			"money":0.01,
			"state":3,//添加
			"h_info":login_name,
			"order_id":order_id,
		},
		dataType : "json",
		success : function(data) {
			console.log(JSON.stringify(data));
			$(".scan_info").attr("src",data[0].URL);
			$(".popup").show();
			//用户是否支付
			countdown=60;
			setInterval('scancode_countdown(\''+order_id+'\')',1000);
		}
	});
}

function scancode_countdown(order_id) { 
	if (countdown == 1) {
		countdown=60;
		popup_hide();
		return false; 
	} else {  
		$("#times").html(countdown+"s"); 
		countdown--; 
		user_ispay(order_id);
	} 
	
}
function user_ispay(order_id){//查询用户是否支付
	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "xmy_zyr.user_ispay",
			para:"['" + order_id + "']"
		},
		dataType: "json",
		success: function(data){
			console.log(JSON.stringify(data));
			if(data[0].A_COUNT=='0'){
				if(countdown==1){
					popup_hide();
				}
			}else{
				top.layer.msg('创建工厂帐号成功!');
				window.location.href="factory_list.html";
			}
		}
	});
}
function popup_hide(){
	$(".popup").hide();
	/*top.layer.alert("创建工厂帐号失败!",function(index){
		countdown=60;
		layer.close(index);
		top.layer.closeAll();
		window.location.href="factory_list.html";
	});*/
	top.layer.msg("创建工厂帐号失败!");		
	returnlist();
}
//返回
function returnlist(){
	//top.layer.closeAll();
	window.location.href="factory_list.html";
}
//校验只能输入汉字，并且长度为2-4
function checkChinese(name) {
	reg = /^[\u4E00-\u9FA5]{2,8}$/;

    if (reg.test(name)) {
		return true;
    } else {
        return false;
    }
 }
 /*  验证百家姓和姓名长度     结束   */